package com.GreenIce.android.util;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.SoundPool;
import android.util.Log;

import com.GreenIce.android.GreenIceActivity;
import com.GreenIce.android.R;

public class SoundEffectManager {	
	SoundPool soundpool;
	int wrong_sound;
	int fool_sound;
	int good_sound;
	int great_sound;
	int excellent_sound;
	int streamVolume;
	int current_playing;
	static SoundEffectManager sem;
	public static SoundEffectManager getInstance() {
		if (sem == null){
			sem = new SoundEffectManager();
		}
		return sem;
	}
	private SoundEffectManager(){
		soundpool = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
		Activity activity = GreenIceActivity.getActivity();
		AudioManager mgr = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);  
		streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
		Log.v("SoundEffectManager","streamVolume:"+streamVolume);
		wrong_sound = soundpool.load(activity, R.raw.wrong, 1);
		fool_sound = soundpool.load(activity, R.raw.fool,1);
		good_sound = soundpool.load(activity, R.raw.good,1);
		great_sound = soundpool.load(activity, R.raw.great,1);
		excellent_sound = soundpool.load(activity, R.raw.excellent,1);		
	}
	public void playFailSound(){
		Log.v("SoundEffectManager","playFailSound");
		current_playing = soundpool.play(wrong_sound, streamVolume, streamVolume, 1, 0, 1);		
	}
	public void playFoolSound(){
		Log.v("SoundEffectManager","playFoolSound");
		current_playing = soundpool.play(fool_sound, streamVolume, streamVolume, 1, 0, 1);
	}
	public void playGoodSound(){
		Log.v("SoundEffectManager","playGoodSound");
		current_playing = soundpool.play(good_sound, streamVolume, streamVolume, 1, 0, 1);
	}
	public void playGreatSound(){
		Log.v("SoundEffectManager","playGreatSound");
		current_playing = soundpool.play(great_sound, streamVolume, streamVolume, 1, 0, 1);
	}
	public void playExcellentSound(){
		Log.v("SoundEffectManager","playExcellentSound");
		current_playing = soundpool.play(excellent_sound, streamVolume, streamVolume, 1, 0, 1);
	}
	private static MediaPlayer mp;
	public static void playClear(){
		mp = MediaPlayer.create(GreenIceActivity.getActivity(), R.raw.clear);
		mp.start();
		mp.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				mp.release();
				mp = null;
			}
		});
	}
	public static void playBigClear(){
		mp = MediaPlayer.create(GreenIceActivity.getActivity(), R.raw.bigclear);
		mp.start();
		mp.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				mp.release();
				mp = null;
			}
		});
	}
	public static void playFail(){
		mp = MediaPlayer.create(GreenIceActivity.getActivity(), R.raw.fail);
		mp.start();
		mp.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				mp.release();
				mp = null;
			}
		});
	}
	public static void releaseMediaplayer(){
		if (mp != null){
//			if (mp.isPlaying()){
//				mp.pause();
//				mp.stop();
//			}
			mp.release();
		}
	}
}
